//发表评论组件
import React, { Component } from 'react'
// import { Comments } from '../Comments'
//导入图片
import avatar from '../images/avatar.jpeg'

class Form extends Component {
  state = {
    txt: ''
  }

  txtRef = React.createRef()

  // onKeyUp = e => {
  //   if (e.keyCode === 13) {
  //     this.findContract()
  //   }
  // }

  handleTxt = e => {
    this.setState({
      txt: e.target.value
    })
  }

  handleAdd = () => {
    //向父组件传值
    this.props.addComment(this.state.txt)
    //更新自己的状态
    this.setState({
      txt: ''
    })
    //设置自己组件的光标,原因是没有控制光标属性,只能通过原生DOM操作
    //这里注意一个细节即是操作DOM也是在React的封装下
    this.txtRef.current.focus()
  }

  render() {
    return (
      //发表评论区
      <div className="comment-send-container">
        <div>
          <img className="user-head" src={avatar} alt="头像" />
        </div>
        <div className="comment-send">
          <div className="textarea-container">
            <textarea
              cols="80"
              rows="5"
              placeholder="发条友善的评论"
              className="ipt-txt"
              value={this.state.txt}
              onChange={this.handleTxt}
              ref={this.txtRef}
              // onKeyUp={this.onKeyUp}
            />
            <button className="comment-submit" onClick={this.handleAdd}>
              发表评论
            </button>
          </div>
          <div className="comment-emoji">
            <i className="face"></i>
            <span className="text">表情</span>
          </div>
        </div>
      </div>
    )
  }
}

export default Form
